package com.h3c.nem.async;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.h3c.nem.client.SapClient;
import com.h3c.nem.entity.TbPersonnelRegisterMiddle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yys44482
 * @Date: 2022/8/2
 */
@Component
@Slf4j
public class AsyncSyncDataToHRSS {

    @Autowired
    SapClient sapClient;

    @Async
    public void saveCompanyRegistrationAddress(TbPersonnelRegisterMiddle tbPersonnelRegisterMiddle, String sapToken){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("SAPCode", tbPersonnelRegisterMiddle.getPersonnelCode());
        jsonObject.put("SAPRange", tbPersonnelRegisterMiddle.getPersonnelScope());
        jsonObject.put("CompanyName", tbPersonnelRegisterMiddle.getCompanyName());
        jsonObject.put("RegistrationAddress", tbPersonnelRegisterMiddle.getRegistration());
        sapClient.saveCompanyRegistrationAddress(jsonObject.toJSONString(), sapToken);
    }

    @Async
    public void deleteCompanyRegistrationAddress(TbPersonnelRegisterMiddle tbPersonnelRegisterMiddle, String sapToken){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("SAPCode", tbPersonnelRegisterMiddle.getPersonnelCode());
        jsonObject.put("SAPRange", tbPersonnelRegisterMiddle.getPersonnelScope());
        jsonObject.put("CompanyName", tbPersonnelRegisterMiddle.getCompanyName());
        jsonObject.put("RegistrationAddress", tbPersonnelRegisterMiddle.getRegistration());
        sapClient.deleteCompanyRegistrationAddress(jsonObject.toJSONString(), sapToken);
    }

    @Async
    public void insertEmpTel(String EmpCode, String Phone, String sapToken){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("EmpCode", EmpCode);
        jsonObject.put("Phone", Phone);
        List<JSONObject> strList = new ArrayList<>();
        strList.add(jsonObject);
        sapClient.insertEmpTel(strList, sapToken);
    }

}
